#!/usr/bin/env python2
# coding=utf-8

import re
import requests

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

def get():
    '''

    '''
    url = r'http://www.soku.com/t/nisearch/{videoName}'.format(videoName=sys.argv[1])
    html = requests.get(url).content
    pattern = re.compile(r'(http://www.tudou.com/albumplay/{vid}/[^"]+.html)'.format(vid=sys.argv[2]))
    url = re.findall(pattern, html)
    #base = r'http://www.tudou.com/albumplay/UbLJt7pKAB4/{id}.html'
    #url = url[10:]
    j = 1
    fp = open("video.txt", "w")
    for i in url:
        # os.system("source ../bin/activate && you-get {html}".format(html=i))
        # os.system("mv 倚天屠龙记.mp4 倚天屠龙记{id}.mp4".format(id=j))
        #print(i)
        fp.write(i+'\n')
        j += 1
    fp.close()

if len(sys.argv) != 3:
    print "Usage:"
    print "\t{m} videoName videoid``".format(m=sys.argv[0])
    print "\t例如：{m} 神话 4k6qfJsiHaQ".format(m=sys.argv[0])
    print "\n"
    print "\tSearch URL: http://www.soku.com/t/nisearch/%E7%A5%9E%E8%AF%9D"
    print "\n"
    print "\tWhat is [videoid] ??"
    print "\t\tThat is each video of videos"
    print "\turl : http://www.tudou.com/albumplay/4k6qfJsiHaQ/sDVLJxfXo38.html"
    print "\tHere: videoid = 4k6qfJsiHaQ"
    # exit(-1)
else:
    get()
